package com.zhx.common.user.controller;


import com.zhx.common.common.domain.dto.RequestInfo;
import com.zhx.common.common.domain.vo.resp.ApiResult;
import com.zhx.common.common.util.AssertUtil;
import com.zhx.common.common.util.RequestHolder;
import com.zhx.common.user.domain.dto.ItemInfoDTO;
import com.zhx.common.user.domain.dto.SummeryInfoDTO;
import com.zhx.common.user.domain.enums.RoleEnum;
import com.zhx.common.user.domain.vo.req.user.*;
import com.zhx.common.user.domain.vo.resp.user.BadgeResp;
import com.zhx.common.user.domain.vo.resp.user.UserInfoResp;
import com.zhx.common.user.service.IRoleService;
import com.zhx.common.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhx
 * @since 2023-12-18
 */
@RestController
@RequestMapping("/capi/user")
@Api(tags = "用户相关接口")
public class UserController {

    @Resource
    private IUserService userService;
    @Resource
    private IRoleService roleService;

    @GetMapping("/userInfo")
    @ApiOperation(value = "获取用户个人信息")
    public ApiResult<UserInfoResp> getUserInfo() {
        RequestInfo requestInfo = RequestHolder.get();
        return ApiResult.success(userService.getUserInfo(requestInfo.getUid()));
    }


    @PutMapping("/name")
    @ApiOperation(value = "修改用户名")
    public ApiResult<Void> modifyName(@Valid @RequestBody ModifyNameReq req) {
        userService.modifyName(RequestHolder.get().getUid(), req.getName());
        return ApiResult.success();
    }

    @GetMapping("/badges")
    @ApiOperation(value = "可选徽章预览")
    public ApiResult<List<BadgeResp>> getBadge() {
        return ApiResult.success(userService.budges(RequestHolder.get().getUid()));
    }

    @PutMapping("/badge")
    @ApiOperation(value = "佩戴徽章")
    public ApiResult<Void> wearBadge(@Valid @RequestBody WearingBadgeReq req) {
        userService.wearBadge(RequestHolder.get().getUid(), req.getBadgeId());
        return ApiResult.success();
    }

    @PutMapping("/black")
    @ApiOperation(value = "拉黑用户")
    public ApiResult<Void> black(@Valid @RequestBody BlackReq req) {
        // 只有超级管理员才能拉黑
        Long uid = RequestHolder.get().getUid();
        boolean b = roleService.hasPower(uid, RoleEnum.ADMIN);
        AssertUtil.isTrue(b, "抹茶普通管理员无权限，请联系超管");

        // 拉黑用户
        userService.black(req.getUid());
        return ApiResult.success();
    }


    //懒加载
    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation("用户聚合信息-返回的代表需要刷新的")
    public ApiResult<List<SummeryInfoDTO>> getSummeryUserInfo(@Valid @RequestBody SummeryInfoReq req) {
        return ApiResult.success(userService.getSummeryUserInfo(req));
    }

    @PostMapping("/public/badges/batch")
    @ApiOperation("徽章聚合信息-返回的代表需要刷新的")
    public ApiResult<List<ItemInfoDTO>> getItemInfo(@Valid @RequestBody ItemInfoReq req) {
        return ApiResult.success(userService.getItemInfo(req));
    }




}

